﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Manage_EditTeacher : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region 初始化年、月
            Year.Items.Clear();
            for (int i = DateTime.Now.Year - 18; i >= 1930; i--)
            {
                Year.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
            }

            Month.Items.Clear();
            for (int i = 1; i <= 12; i++)
            {
                Month.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
            }
            #endregion

            #region 初始化民族
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Server.MapPath("~/Data/Nation.xml");
            xds.XPath = "Nations/Nation";

            Nation.DataSource = xds;
            Nation.DataTextField = "name";
            Nation.DataValueField = "name";
            Nation.DataBind();
            #endregion

            #region 初始化政治面貌
            xds = new XmlDataSource();
            xds.DataFile = Server.MapPath("~/Data/Policital.xml");
            Policital.DataSource = xds;
            Policital.DataMember = "Policital";
            Policital.DataTextField = "name";
            Policital.DataValueField = "name";
            Policital.DataBind();
            #endregion

            #region 初始化学历
            xds = new XmlDataSource();
            xds.DataFile = Server.MapPath("~/Data/Education.xml");
            Education.DataSource = xds;
            Education.DataMember = "Education";
            Education.DataTextField = "name";
            Education.DataValueField = "name";
            Education.DataBind();
            #endregion

            #region 初始化课程
            xds = new XmlDataSource();
            xds.DataFile = Server.MapPath("~/Data/Course.xml");
            xds.XPath = "Root/Kind/Subject/Course";
            Courses.DataSource = xds;
            Courses.DataTextField = "name";
            Courses.DataValueField = "name";
            Courses.DataBind();
            #endregion

            if (Request.Params["tid"] != null)
            {
                TeacherID.Value = Request.Params["tid"];
                Username.ReadOnly = true;

                Teacher teacher;
                #region 加载教师信息
                CmsDataContext db = new CmsDataContext();
                teacher = (from aT in db.Teacher
                           where aT.ID == TeacherID.Value.Trim()
                           select aT).First();

                Name.Text = teacher.Name;
                Gender.SelectedValue = teacher.Gender;
                Nation.SelectedValue = teacher.Nation;
                Policital.SelectedValue = teacher.Policital;
                Health.Text = teacher.Health;
                Unit.Text = teacher.Unit;
                Address.Text = teacher.Address;
                Office.Text = teacher.Office;
                Mobile.Text = teacher.Mobile;
                Home.Text = teacher.Home;
                Email.Text = teacher.Email;
                Education.SelectedValue = teacher.Education;
                Duty.Text = teacher.Duty;
                Profession.Text = teacher.Profession;
                Memo.Text = teacher.Memo;

                String[] cs = teacher.Courses.Split('|');
                foreach (string c in cs[0].Split(','))
                {
                    foreach (ListItem li in Courses.Items)
                    {
                        if (li.Value == c)
                        {
                            li.Selected = true;
                        }
                    }
                }

                Others.Text = cs[1];

                Username.Text = teacher.Managers.First().ID;

                Picture.ImageUrl = teacher.Photo;
                Photo.Value = teacher.Photo;

                Year.SelectedValue = Convert.ToString(teacher.Birthday.Value.Year);
                Month.SelectedValue = Convert.ToString(teacher.Birthday.Value.Month);
                #endregion


                Rank.SelectedValue = teacher.Rank;
                Save.Visible = (teacher.Verfied == null) && Toolkit.HasPower(Powers.TeacherManager) && (Toolkit.CurrentUserBelong == teacher.Belong);
                Verified.Visible = (teacher.Verfied == null) && Toolkit.HasPower(Powers.TeacherVerifier) && ((Toolkit.CurrentUserBelong == teacher.Belong) || Toolkit.IsChildBelong(Toolkit.CurrentUserBelong, teacher.Belong));

                ResetPasswordLink.Visible = Toolkit.HasPower(Powers.TeacherManager) && (teacher.District == Toolkit.CurrentUserDistrict) && (teacher.Belong == Toolkit.CurrentUserBelong);

                if (ResetPasswordLink.Visible)
                {
                    ResetPasswordLink.HRef = "javascript:resetPassword('" + teacher.Managers.First().ID + "');";
                }
            }
            else
            {
                Save.Visible = true;
                Verified.Visible = false;
                ResetPasswordLink.Visible = false;
            }
        }
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        string teacherid = TeacherID.Value.Trim();
        CmsDataContext db = new CmsDataContext();

        try
        {
            if (teacherid == "")
            {
                #region 验证用户登录名是否存在
                var q1 = from aM in db.Manager
                         where aM.ID == Username.Text.Trim()
                         select aM;
                if (q1.Count() != 0)
                {
                    //提示用户登录名已经存在
                    MessageBox.Message = "教师登录名已经存在，请选择其它的登录名。";
                    return;
                }
                #endregion
            }


            Teacher teacher;

            #region 新增或更新教师信息
            var q = from aT in db.Teacher
                    where aT.ID == teacherid
                    select aT;
            if (q.Count() == 0)
            {
                teacher = new Teacher();
                teacher.ID = Toolkit.NextUUID;

                db.Teacher.InsertOnSubmit(teacher);
            }
            else
            {
                teacher = q.First();
            }

            teacher.Name = Name.Text.Trim();
            teacher.Gender = Gender.SelectedValue;
            teacher.Nation = Nation.SelectedValue;
            teacher.Birthday = Convert.ToDateTime(string.Format("{0}-{1}-01", Year.SelectedValue, Month.SelectedValue));
            teacher.Policital = Policital.SelectedValue;
            teacher.Health = Health.Text.Trim();
            teacher.Unit = Unit.Text.Trim();
            teacher.Address = Address.Text.Trim();
            teacher.Office = Office.Text.Trim();
            teacher.Mobile = Mobile.Text.Trim();
            teacher.Home = Home.Text.Trim();
            teacher.Email = Email.Text.Trim();
            teacher.Education = Education.SelectedValue;
            teacher.Duty = Duty.Text.Trim();
            teacher.Profession = Profession.Text.Trim();
            teacher.Memo = Memo.Text.Trim();
            teacher.Rank = Rank.SelectedValue;
            teacher.Photo = Photo.Value.Trim();

            StringBuilder sb = new StringBuilder();
            foreach (ListItem li in Courses.Items)
            {
                if (li.Selected)
                {
                    sb.Append(((sb.ToString() == "") ? "" : ",") + li.Value);
                }
            }

            sb.Append("|" + Others.Text.Trim());

            teacher.Courses = sb.ToString();

            teacher.District = Toolkit.CurrentUserDistrict;
            teacher.Belong = Toolkit.CurrentUserBelong;
            #endregion


            #region 在Manage表增加教师登录信息
            var q3 = from aM in db.Manager
                     where aM.ID == Username.Text.Trim()
                     select aM;

            Manager m;
            if (q3.Count() == 0)
            {
                m = new Manager();
                m.ID = Username.Text.Trim();
                m.TeacherID = teacher.ID;
                m.Password = Toolkit.EncryptPassword(Toolkit.InitialPassword);

                db.Manager.InsertOnSubmit(m);
            }
            else
            {
                m = q3.First();
            }

            m.Name = teacher.Name;
            m.District = teacher.District;
            m.Belong = teacher.Belong;
            #endregion

            db.SubmitChanges();

            MessageBox.Message = "教师信息保存成功。";
            MessageBox.Location = "./EditTeacher.aspx?tid=" + teacher.ID;
        }
        catch (Exception ee)
        {
            MessageBox.Message = "保存教师信息时发生错误。" + ee.Message;
        }
    }
    protected void Verified_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();
        string tid = Request.Params["tid"];

        try
        {
            Teacher teacher = (from aT in db.Teacher
                               where aT.ID == tid
                               select aT).First();

            teacher.VerfierID = Toolkit.CurrentUserID;
            teacher.Verfied = DateTime.Now;

            db.SubmitChanges();

            MessageBox.Message = "教师信息已经通过审核。";
            MessageBox.Location = "./EditTeacher.aspx?tid=" + tid;
        }
        catch (Exception ee)
        {
            MessageBox.Message = ee.Message;
            //MessageBox.Location = "./EditTeacher.aspx?tid=" + tid;
        }
    }
}